package org.vectrics.recruiter.struts.admin;

import java.util.Iterator;

import javax.servlet.jsp.JspException;

import org.apache.log4j.Category;
import org.apache.struts.action.ActionMapping;
import org.vectrics.domain.Domain;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.config.RecruitConfig;
import org.vectrics.recruiter.user.ResumeScanBatch;
import org.vectrics.recruiter.user.ResumeScanCriteria;
import org.vectrics.struts.Request;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;


public class PrepareResumeScanFormAction extends VectricsStrutsAction {
    private static Category log = Category.getInstance(PrepareResumeScanFormAction.class);
	public static String JOB_ID_PARAM = "jobId";


	// Intercept 
	public String doExecute(VectricsActionData actionData)	throws JspException {
		log.debug("execute()");
		String batchId = actionData.getRequest().getParameter("batchId");
		String jobId = actionData.getRequest().getParameter("jobId");
		doWork(batchId, jobId, actionData.getRequest(), actionData.getMapping());
		return ("success");
	}

	public static void doWork(String batchId, String jobId, Request request, ActionMapping mapping) {
		ResumeScanForm scanForm = new ResumeScanForm();
		Domain domain = ThreadProperties.getDomain();
		if (batchId == null) {
		    scanForm.setBatchId(null);
		    scanForm.setMaxResultCount("" + RecruitConfig.getRecruitInstance().getResumeScanMaxCount(domain));
		    scanForm.setMinResultCount("" + RecruitConfig.getRecruitInstance().getResumeScanMinCount(domain));
		    scanForm.setMinPercentage("" + RecruitConfig.getRecruitInstance().getResumeScanMinPercentCutoff(domain));
		    scanForm.setShared("false");
		} else {
		    ResumeScanBatch batch = RecruitServiceLocator.getRecruitUserService().findResumeScanBatch(new Long(batchId));
		    scanForm.setBatchId(batch.getId().toString());
		    scanForm.setShared("" + batch.getShared());
		    scanForm.setDescription(batch.getDescription());
		    scanForm.setMaxResumeAge(batch.getMaxResumeAge().toString());
		    scanForm.setMaxResultCount("" + batch.getMaximumCount());
		    scanForm.setMinResultCount("" + batch.getMinimumCount());
		    scanForm.setMinPercentage("" + batch.getMinimumPercent());
		    Iterator iter = batch.getCriteriaList().iterator();
		    int critIndex = 0;
		    while (iter.hasNext()) {
		        ResumeScanCriteria criteria = (ResumeScanCriteria)iter.next();
		        writeCriteriaToForm(scanForm, criteria, critIndex);
		        critIndex++;
		    }
		}
	    scanForm.setJobId(jobId);
		request.setAttribute(mapping.getAttribute(), scanForm);
	}
	
    private static void writeCriteriaToForm(ResumeScanForm form, ResumeScanCriteria criteria, int critIndex) {
        if (critIndex == 0) {
            form.setWord1(criteria.getPhrase());
            form.setWeight1(criteria.getWeight().toString());
        } else if (critIndex == 1) {
            form.setWord2(criteria.getPhrase());
            form.setWeight2(criteria.getWeight().toString());
        } else if (critIndex == 2) {
            form.setWord3(criteria.getPhrase());
            form.setWeight3(criteria.getWeight().toString());
        } else if (critIndex == 3) {
            form.setWord4(criteria.getPhrase());
            form.setWeight4(criteria.getWeight().toString());
        } else if (critIndex == 5) {
            form.setWord6(criteria.getPhrase());
            form.setWeight6(criteria.getWeight().toString());
        } else if (critIndex == 6) {
            form.setWord7(criteria.getPhrase());
            form.setWeight7(criteria.getWeight().toString());
        } else if (critIndex == 7) {
            form.setWord8(criteria.getPhrase());
            form.setWeight8(criteria.getWeight().toString());
        }    }

		
}
